﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;

namespace TerrainProcessor
{
    public class HeightmapInfoContent
    {
        public float[,] height;
        public float terrainScale;
        public HeightmapInfoContent(PixelBitmapContent<float> bitmap, float terrainScale, float terrainBumpiness)
        {
            this.terrainScale = terrainScale;

            height = new float[bitmap.Width, bitmap.Height];
            for (int y = 0; y < bitmap.Height; y++)
            {
                for (int x = 0; x < bitmap.Width; x++)
                {
                    height[x, y] = (bitmap.GetPixel(x, y) - 1) * terrainBumpiness;
                }
            }
        }
    }

    [ContentTypeWriter]
    public class HeightMapInfoWriter : ContentTypeWriter<HeightmapInfoContent>
    {
        protected override void Write(ContentWriter output, HeightmapInfoContent value)
        {
            output.Write(value.terrainScale);
            output.Write(value.height.GetLength(0));
            output.Write(value.height.GetLength(1));
            foreach (float height in value.height)
            {
                output.Write(height);
            }

        }

        public override string GetRuntimeType(TargetPlatform targetPlatform)
        {
            return "TerrainProcessor.HeightMapInfo, " + "TerrainProcessor, Version=1.0.0.0, Culture=neutral";
        }

        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {
            return "TerrainProcessor.HeightMapInfoReader, " + "TerrainProcessor, Version=1.0.0.0, Culture=neutral";
        }

    }

}
